/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_DoanTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_DoanTau {
    DataProvider data;
    
    public List<POJO_DoanTau> LoadList() throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM doan_tau";
        List<POJO_DoanTau> lst = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
            
        while (rs.next()) {
            POJO_DoanTau dt = new POJO_DoanTau();
            
            dt.setMADT(rs.getString("MaDT"));
            dt.setTENDT(rs.getString("TenDT"));
            dt.setHANGSX(rs.getString("HangSX"));
            dt.setNGAYVANHANH(rs.getDate("NgayVanHanh"));
            dt.setLOAITAU(rs.getString("LoaiTau"));
        
            lst.add(dt); 
        }
        
        return lst;
    } 
    
    public int xoaDoanTau(POJO_DoanTau dt) {
            String sql = String.format("DELETE FROM doan_tau WHERE MaDT = '%s'", dt.getMADT());
            data = new DataProvider();
            data.Open();
            
            return data.ExeCuteUpdate(sql);
    }
    
    public int themDoanTau(POJO_DoanTau dt) {
        data = new DataProvider();
        data.Open();
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String sql = String.format("Insert into doan_tau values ('%s','%s','%s','%s','%s')", dt.getMADT(), dt.getTENDT(), dt.getHANGSX(), dt.getNGAYVANHANH(), dt.getLOAITAU());
            
        return data.ExeCuteUpdate(sql);
    } 
    
    public int capNhatDoanTau(POJO_DoanTau dt)  {
        data = new DataProvider();
        data.Open();
        
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String sql = String.format("UPDATE doan_tau SET TenDT = '%s', HangSX = '%s', NgayVanHanh = '%s', LoaiTau= '%s' where MaDT = '%s'",  
                    dt.getTENDT(),
                    dt.getHANGSX(),
                    df.format(dt.getNGAYVANHANH()),
                    dt.getLOAITAU(),
                    dt.getMADT());        
            
            
        return data.ExeCuteUpdate(sql);
    }
}
